
package bsc.war.handler;

import bsc.ejb.entity.Person;
import bsc.ejb.entity.PersonScoreDomain;
import bsc.ejb.entity.ScoreDomain;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.BSCUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author memozac
 */
public class UserDomain extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            String uri = request.getParameter("uri");
            
            PersonScoreDomain personScoreDomain = processRequestInternal(request, response, delegate);
            
            return new ModelAndView(new RedirectView(uri == null ? BSCUtils.getURI(request, personScoreDomain) : request.getContextPath() + uri));
            
        } catch (Exception e) { logger.error("Catched exception",e); e.printStackTrace(); }

        return new ModelAndView("../index");
    }
    public static PersonScoreDomain processRequestInternal(HttpServletRequest request, HttpServletResponse response, DelegateBSC delegate){
        Long idScoreDomain = Long.parseLong(request.getParameter("scoreDomain"));
        ScoreDomain scoreDomain = delegate.findScoreDomain(idScoreDomain);
        Person person = (Person)request.getSession(true).getAttribute("person");

        PersonScoreDomain personScoreDomain = delegate.findPersonScoreDomain(person, scoreDomain);
        BSCUtils.setSessionContent(request, delegate, personScoreDomain);
        return personScoreDomain;
    }
}